package com.itheima.test;

import com.itheima.mentalarithmeticpracticesystem.*;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 程序入口
 */
public class MainApp {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        ArrayList<User> userArrayList = new ArrayList<>();        //创建成员集合
        userArrayList.add(new Teacher("太乙真人", "123456"));
        userArrayList.add(new Student("哪吒", "123456"));

        ArrayList<MathTest> mathTestArrayList = new ArrayList<>();    //创建题库集合
        mathTestArrayList.add(new AddTest(4, 5));//加法
        mathTestArrayList.add(new SubtractTest(9, 6));//减法
        mathTestArrayList.add(new MultiplyTest(3, 8));//乘法
        mathTestArrayList.add(new DivideTest(6, 2));//除法


        System.out.println("欢迎来到黑马小学生算术练习系统");
        System.out.println("请输入账号");
        String loginName = scanner.next();
        System.out.println("请输入密码");
        String loginPassWorld = scanner.next();

        for (int i = 0; i < userArrayList.size(); i++) {
            if (userArrayList.get(i).getName().equals(loginName) && userArrayList.get(i).getPassword().equals(loginPassWorld)) {

                if (userArrayList.get(i) != null) {
                    if (userArrayList.get(i) instanceof Student) {      //判断 索引到的集合对象是否属于Student
                        //调用学生登录页面
                        showStudentUI(scanner, userArrayList.get(i), mathTestArrayList);

                    } else if (userArrayList.get(i) instanceof Teacher) {    //判断 索引到的集合对象是否属于Teacher
                        //调用老师登录页面
                        showTeacherUI(scanner, userArrayList.get(i), mathTestArrayList);
                    }
                }
            }

        }


    }


    //添加题目
    private static void addTest(Scanner scanner, ArrayList<MathTest> mathTests) {
        String ra = "Y";
        while (true) {
            System.out.println("是否继续添加题目，是请按Y，否则任意输入");
            ra = scanner.next();
            if (ra.equals("Y")) {
                //-----------------
                System.out.println("请选择需要添加的题型：");
                System.out.println("1:加法题\n" + "2:减法题\n" + "3:乘法题\n" + "4:除法题");
                int num = scanner.nextInt();
                switch (num) {
                    case 1:
                        System.out.println("请输入加数");
                        int x = scanner.nextInt();
                        System.out.println("请输入被加数");
                        int y = scanner.nextInt();
                        AddTest addTest = new AddTest(x, y);
                        mathTests.add(addTest);
                        System.out.println("题目：" + addTest.getQuestion() + "  添加成功！");

                        break;
                    case 2:
                        System.out.println("请输入减数");
                        int x2 = scanner.nextInt();
                        System.out.println("请输入被减数");
                        int y2 = scanner.nextInt();
                        SubtractTest subtractTest = new SubtractTest(x2, y2);
                        mathTests.add(subtractTest);
                        System.out.println("题目：" + subtractTest.getQuestion() + "  添加成功！");
                        break;
                    case 3:
                        System.out.println("请输入加数");
                        int x3 = scanner.nextInt();
                        System.out.println("请输入被加数");
                        int y3 = scanner.nextInt();
                        MultiplyTest multiplyTest = new MultiplyTest(x3, y3);
                        mathTests.add(multiplyTest);
                        System.out.println("题目：" + multiplyTest.getQuestion() + "  添加成功！");
                        break;
                    case 4:
                        System.out.println("请输入加数");
                        int x4 = scanner.nextInt();
                        System.out.println("请输入被加数");
                        int y4 = scanner.nextInt();
                        DivideTest divideTest = new DivideTest(x4, y4);
                        mathTests.add(divideTest);
                        System.out.println("题目：" + divideTest.getQuestion() + "  添加成功！");
                        break;
                    default:
                        System.out.println("请输入有效的功能码");
                        break;
                }
                //------------------
            } else {
                System.out.println("是否继续添加题目，是请按Y，否则任意输入");
                ra = scanner.next();

                if (!ra.equals("Y")) {
                    return;
                }
            }

        }//while


    }


    //答题
    public static int answer(Scanner scanner, ArrayList<MathTest> mathTests) {
        System.out.println("您好，一共" + mathTests.size() + "道题目，请开始作答，答对一题得1分，答错不给分" + "\n"
                + "请回答以下问题，机会只有一次：\n");

        //创建积分器
        int counts = 0;
        for (int i = 0; i < mathTests.size(); i++) {
            System.out.println(mathTests.get(i).getQuestion().toString());
            int num = scanner.nextInt();
            if (num == mathTests.get(i).calculate()) {
                counts++;
                System.out.println("回答正确！");
            } else {
                System.out.println("回答错误！继续努力");
            }
        }


        return counts;
    }

    //---------------
    //删除题目
    private static void deleteTest(Scanner scanner, User user, ArrayList<MathTest> mathTests) {
        while (true) {
            System.out.println("当前的题库如下");
            for (int i = 0; i < mathTests.size(); i++) {
                System.out.println("题目:" + (i + 1) + "  " + mathTests.get(i) + "    参考答案：" + mathTests.get(i).calculate());
            }
            System.out.println("请输入您想删除的题号");
            int index = scanner.nextInt() - 1;
            if (index >= 0 && index <= mathTests.size() - 1) {
                mathTests.remove(index);
                System.out.println("删除成功");
                System.out.println("是否继续删除，继续的话请输入”Y”,退出输入其他");
                String ra = scanner.next();
                if (!ra.equals("Y")) {
                    return;
                }
            } else {
                System.out.println("您输入的功能码错误");
            }
        }


    }

    //学生登录入口
    private static void showStudentUI(Scanner scanner, User user, ArrayList<MathTest> mathTests) {
        System.out.println("欢迎" + user.getName() + "同学进入系统！");

        String ran = "Y";

        do {
            int num = answer(scanner, mathTests);
            System.out.println("一共" + mathTests.size() + "道题，每题一分，共得分" + num);
            if (num==4){
                System.out.println("哇哦！算术这方面，你就是王者！佩服你" );
            }else if (num==3){
                System.out.println("哇哦！算术这方面，你就是大师！佩服你" );
            }else if (num==2){
                System.out.println("哇哦！算对了一半，很不错了！再接再厉" );
            }else if (num==1){
                System.out.println("人生总会有失败，再接再厉" );
            }else if (num==0){
                System.out.println( "小弟弟，真确率惨不忍睹哦！");
            }
            System.out.println( "成绩不服，输入Y继续挑战，否则输入其它退出！");
            ran = scanner.next();

        } while (ran.equals("Y"));

    }

    //老师登录入口
    private static void showTeacherUI(Scanner scanner, User user, ArrayList<MathTest> mathTests) {

        System.out.println("欢迎" + user.getName() + "老师进入系统！" + "\n" +
                "当前题库如下：\n：");
        for (int i = 0; i < mathTests.size(); i++) {
            System.out.println("问题是 " + mathTests.get(i).getQuestion() + "\t" + "    当前的参考答案为 " + mathTests.get(i).calculate());
        }

        System.out.println("是否要编辑题目,需要编辑输入Y,输入其他无需修改，并退出登录！");
        String ra = scanner.next();
        if (ra.equals("Y") == true) {
            System.out.println("1、添加题目\n" + "2、删除题目\n");
            int num = scanner.nextInt();
            switch (num) {
                case 1:
                    addTest(scanner, mathTests);
                    break;
                case 2:
                    deleteTest(scanner, user, mathTests);
                    break;
                default:
                    System.out.println("您输入的功能码错误");
                    break;
            }
        } else {
            return;
        }

    }


}
